/*
 * ChannelStreetLightTests.java
 *
 * Created on Jun 9, 2007, 10:26:12 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.infomancers.workflow;

import com.infomancers.workflow.channels.ChannelFactory;
import com.infomancers.workflow.concrete.ConcretePlace;
import com.infomancers.workflow.dynamic.WorkflowBuilder;
import com.infomancers.workflow.tests.DebuggingFlowManager;
import com.infomancers.workflow.tests.SocketChannelFactory;
import java.io.IOException;
import junit.framework.JUnit4TestAdapter;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author aviadbd
 */
public class ChannelStreetLightTests {
    
    private Workflow createSourceWorkflow(ChannelFactory channel) {
        WorkflowBuilder builder = new WorkflowBuilder();
        Place input = new ConcretePlace("input");
        Place process = new ConcretePlace("process-local");
        
        Transition transition = new Transition();
        
        builder.addConnection(input, transition);
        builder.addConnection(transition, process);
        
        builder.addConnection(process, channel);
        
        return builder.createWorkflow(input, null, new DebuggingFlowManager());
    }
    
    private Workflow createTargetWorkflow(ChannelFactory channel) {
        WorkflowBuilder builder = new WorkflowBuilder();
        Place output = new ConcretePlace("output");
        Place process = new ConcretePlace("process-remote");
        
        Transition transition = new Transition();
        
        builder.addConnection(channel, process);
        
        builder.addConnection(process, transition);
        builder.addConnection(transition, output);
        
        
        return builder.createWorkflow(null, output, new DebuggingFlowManager());
    }
    
    private ChannelFactory createChannel() {
        return new SocketChannelFactory(5123);
    }
    
    @Test
    public void creation() throws IOException {
        SocketChannelFactory channel = (SocketChannelFactory) createChannel();
        assertNotNull(channel);
        Workflow source = createSourceWorkflow(channel);
        assertNotNull(source);
        Workflow target = createTargetWorkflow(channel);
        assertNotNull(target);
        
        channel.close();
    }
    
    @Test
    public void simpleTransfer() throws IOException {
        SocketChannelFactory channel = (SocketChannelFactory) createChannel();
        Workflow source = createSourceWorkflow(channel);
        Workflow target = createTargetWorkflow(channel);
        
        WorkflowQueue sourceQ = new WorkflowQueue(source);
        WorkflowQueue targetQ = new WorkflowQueue(target);
        
        sourceQ.pushData(1);
        assertEquals(targetQ.pullData(), 1);
        
        channel.close();
    }
    
    @Test
    public void multipleSimpleTransfers() throws IOException {
        SocketChannelFactory channel = (SocketChannelFactory) createChannel();
        Workflow source = createSourceWorkflow(channel);
        Workflow target = createTargetWorkflow(channel);
        
        WorkflowQueue sourceQ = new WorkflowQueue(source);
        WorkflowQueue targetQ = new WorkflowQueue(target);
        
        sourceQ.pushData(1);
        assertEquals(targetQ.pullData(), 1);
        sourceQ.pushData(2);
        assertEquals(targetQ.pullData(), 2);
        sourceQ.pushData(3);
        assertEquals(targetQ.pullData(), 3);
        
        channel.close();
    }
    
    
    public static junit.framework.Test suite() {
        return new JUnit4TestAdapter(ChannelStreetLightTests.class);
    }
}
